package com.houweishi.module.clockbinding.service.impl;

import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.common.utils.bean.BeanUtils;
import com.houweishi.module.clockbinding.domain.AllClockingBindVO;
import com.houweishi.module.clockbinding.domain.BizClockingBinding;
import com.houweishi.module.clockbinding.mapper.BizClockingBindingMapper;
import com.houweishi.module.clockbinding.service.IBizClockingBindingService;
import com.houweishi.module.clockedtype.domain.BizClockedType;
import com.houweishi.module.clockedtype.mapper.BizClockedTypeMapper;
import com.houweishi.module.clockscope.domain.BizClockingScope;
import com.houweishi.module.clockscope.mapper.BizClockingScopeMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 员工与打卡类型绑定Service业务层处理
 * 
 * @author houweishi
 * @date 2023-03-02
 */
@Service
public class BizClockingBindingServiceImpl implements IBizClockingBindingService 
{
    @Resource
    private BizClockingBindingMapper bizClockingBindingMapper;

    @Resource
    private BizClockingScopeMapper bizClockingScopeMapper;

    /**
     * 打卡类型
     */
    @Resource
    private BizClockedTypeMapper bizClockedTypeMapper;

    /**
     * 查询员工与打卡类型绑定
     * 
     * @param id 员工与打卡类型绑定主键
     * @return 员工与打卡类型绑定
     */
    @Override
    public BizClockingBinding selectBizClockingBindingById(Long id)
    {
        return bizClockingBindingMapper.selectBizClockingBindingById(id);
    }

    /**
     * 查询员工与打卡类型绑定列表
     * 
     * @param bizClockingBinding 员工与打卡类型绑定
     * @return 员工与打卡类型绑定
     */
    @Override
    public List<BizClockingBinding> selectBizClockingBindingList(BizClockingBinding bizClockingBinding) {
        List<BizClockingBinding> list = bizClockingBindingMapper.selectBizClockingBindingList(bizClockingBinding);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(binding -> {
                //通过改员工在职状态新增的员工绑定模型，还没有加上模型id
                if (binding.getClockTypeId() != null) {
                    List<BizClockingScope> bizClockingScopeList = getBizClockingScopes(binding);
                    binding.setScopeList(bizClockingScopeList);
                }
            });
        }
        return list;
    }

    /**
     * 新增员工与打卡类型绑定
     * 
     * @param bizClockingBinding 员工与打卡类型绑定
     * @return 结果
     */
    @Override
    public AjaxResult insertBizClockingBinding(BizClockingBinding bizClockingBinding)
    {
        bizClockingBinding.setCreateTime(DateUtils.getNowDate());
        //判断是否已经存在，存在则不新增，不存在则新增
        BizClockingBinding entity = new BizClockingBinding();
        entity.setStaffId(bizClockingBinding.getStaffId());
        List<BizClockingBinding> list = bizClockingBindingMapper.selectBizClockingBindingList(entity);
        if (CollectionUtils.isEmpty(list)){
            bizClockingBindingMapper.insertBizClockingBinding(bizClockingBinding);
        }
        return AjaxResult.success();
    }

    /**
     * 修改员工与打卡类型绑定
     * 
     * @param bizClockingBinding 员工与打卡类型绑定
     * @return 结果
     */
    @Override
    public int updateBizClockingBinding(BizClockingBinding bizClockingBinding)
    {
        bizClockingBinding.setUpdateTime(DateUtils.getNowDate());
        return bizClockingBindingMapper.updateBizClockingBinding(bizClockingBinding);
    }

    /**
     * 批量删除员工与打卡类型绑定
     * 
     * @param ids 需要删除的员工与打卡类型绑定主键
     * @return 结果
     */
    @Override
    public int deleteBizClockingBindingByIds(Long[] ids)
    {
        return bizClockingBindingMapper.deleteBizClockingBindingByIds(ids);
    }

    /**
     * 删除员工与打卡类型绑定信息
     * 
     * @param id 员工与打卡类型绑定主键
     * @return 结果
     */
    @Override
    public int deleteBizClockingBindingById(Long id)
    {
        return bizClockingBindingMapper.deleteBizClockingBindingById(id);
    }

    /**
     * 查询员工打卡配置信息
     * @param staffId
     * @return
     */
    @Override
    public AjaxResult getStaffClockInfo(Long staffId) {
        AllClockingBindVO allClockingBindVO = new AllClockingBindVO();
        BizClockingBinding bizClockingBinding = new BizClockingBinding();
        bizClockingBinding.setStaffId(staffId);
        //获取员工绑定信息
        List<BizClockingBinding> list = bizClockingBindingMapper.selectBizClockingBindingList(bizClockingBinding);
        if (!CollectionUtils.isEmpty(list)){
            BizClockingBinding vo = list.get(0);
            BeanUtils.copyProperties(vo,allClockingBindVO);
            if (!Objects.isNull(vo.getClockTypeId())){
                //获取打卡配置时间
                allClockingBindVO.setScopeList(getBizClockingScopes(vo));
                // 获取打卡经纬
                BizClockedType bizClockedType = bizClockedTypeMapper.selectBizClockedTypeById(vo.getClockTypeId());
                if (Objects.nonNull(bizClockedType)){
                    allClockingBindVO.setBizClockedType(bizClockedType);
                }

            }
        }
        return AjaxResult.success(allClockingBindVO);
    }

    /**
     * 获取打卡配置时间
     * @param vo
     * @return
     */
    private List<BizClockingScope> getBizClockingScopes(BizClockingBinding vo) {
        BizClockingScope clockingScope = new BizClockingScope();
        clockingScope.setTypeId(vo.getClockTypeId());
        List<BizClockingScope> scopeList = bizClockingScopeMapper.selectBizClockingScopeList(clockingScope);
        if (CollectionUtils.isEmpty(scopeList)){
            return null;
        }
        return scopeList;
    }
}
